
print("冒泡排序")
source_list=[3,2,1,4,7,2,33,55]
print(source_list)
for i in range(0,len(source_list)):
    for j in range(0,len(source_list)-1-i):
        if source_list[j]<source_list[j+1]:
            source_list[j],source_list[j+1]=source_list[j+1],source_list[j]
print(source_list)

print("求出100到1000之间每个位上的3次方的和等于其自身的数")
res=[]
for i in range(100,1001):
    tem=sum([int(i)**3 for i in list(str(i))])
    if tem==i:
        res.append(i)
print(res)

print("修饰器")
def xiushiqi(func):
    def wrapper(*args,**kwargs):
        print("方法前执行")
        func(*args,**kwargs)
        print("方式后执行")
    return wrapper
@xiushiqi
def demo_001():
    print("待测试方法")
demo_001()

print("单例模式")
import threading as td
class SingleTon:
    lock=td.Lock()
    INSTANCE=None
    def __new__(cls, *args, **kwargs):
        if not cls.INSTANCE:
            with cls.lock:
                if not cls.INSTANCE:
                    cls.INSTANCE=super().__new__(cls,*args,**kwargs)

instance001=SingleTon()
instance002=SingleTon()
print(instance001==instance002)

print("判断字符串是否存在于字符串中")
print("a" in "abc")
print("查找字符串，返回第一个出现的下标find  index  rfind")
sentence="Python is awesome.123123123Python is dynamic."
first_occurrence_find=sentence.find("Python")
first_occurrence_index=sentence.find("Python")
first_occurrence_right_find=sentence.rfind("Python")
print(first_occurrence_find,first_occurrence_index,first_occurrence_right_find)
# 使用find或rfind方法，不存在的话则返回-1
print(sentence.find("xxx"))
print(sentence.rfind("yyy"))

print("异常处理  try except else finally")
# 使用index的方法，如果字符串中不存在则会报错
try:
    # print(sentence.index("is"))
    print(sentence.index("xxx"))
# except Exception:
except ValueError:
    print("子字符串不存在")
else:
    print("如果没有发生异常，执行这段代码")
finally:
    print("不管有没有异常都执行这段代码")


print("capitalize() 方法将字符串的首字母大写，title() 方法将字符串每个单词的首字母大写")
text="hello world"
text002="hello , world"
print(text.capitalize())
print(text002.capitalize())
print(text.title())
print(text002.title())

# 计算非重复元素个数：不使用封装好的方法
def caculate_longest_substring_length(str):
    if not str:
        return 0
    left=0
    max_length=0
    char_index={}
    for right in range(0,len(str)):
        if str[right] in char_index:
            left=char_index.get(str[right])+1
        # setdefault
        # 方法的问题在于，如果键已经存在，它不会更新值，而是返回已存在的值
        # char_index.setdefault(str[right],right)
        char_index[str[right]]=right
        max_length=max(max_length,right-left+1)

        # if str[right] in char_index:
        #     left=char_index[str[right]]+1
        # char_index[str[right]]=right
        # max_length=max(max_length,right-left+1)

    return max_length
print(caculate_longest_substring_length("abcbcbb"))
print(caculate_longest_substring_length("abcabcdbb"))
print(caculate_longest_substring_length("aa"))
print(caculate_longest_substring_length("abc"))
print("使用封装好的方法求非重复元素个数")
print(set(list("abcabcbb")))
print(set(list("abcabcdbb")))
print(set(list("aa")))
print(set(list("abc")))


print("最长公共前缀")
def zuichangqianzhui(strs:list[str]):
    strs.sort(key=lambda s:len(s))
    num=len(strs[0])
    res=""
    for i in range(1,num):
        print(i)
        flag=False
        for ss in strs:
            if not ss.startswith(strs[0][0:i]):
                flag=True
        if flag:
            return res
        else:
            res=strs[0][0:i+1]
    return res
print(zuichangqianzhui(["flower","flow"]))
print("flow"[0:3])

print("最长公共前缀2")
def  zuichagngonggongqianzhui(strs:list[str]):
    strs.sort(key=lambda s:len(s))
    prefix=""
    for i in range(0,len(strs[0])):
        tem=len(set(ss[i] for ss in strs))
        if tem==1:
            prefix=prefix+strs[0][i]
        else:
            return prefix
    return prefix
print(zuichagngonggongqianzhui(["flower","flow","flight"]))

